package me.hl.stream;

import me.hl.lambda.Book;
import org.apache.commons.io.IOUtils;
import org.junit.Test;

import java.io.*;

/**
 * Created by Planck on 2016/2/15.
 */
public class StreamDemo {

    @Test
    public void InputStreamTest() throws Exception {
        File file = new File("D://a");
        long len = file.length();
        byte[] data = new byte[(int) len];
        FileInputStream inputStream = new FileInputStream(file);
        //可以通过inputStreamReader转化inputStream
        //BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        //不带缓冲的话读一个字节就需要写入一个字节。效率很低。使用带有缓冲的bufferedinputStream效率很高
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        bufferedInputStream.read(data);
    }


    @Test
    //这种方法似乎没有办法设置字符集。所以如果出现字符问题会导致乱码
    public void bufferReaderTest() {
        File file = new File("F://a.txt");
        File file1 = new File("F://b.txt");
        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter = null;
        try {
            FileReader fileReader = new FileReader(file);
            FileWriter fileWriter = new FileWriter(file1);
            bufferedReader = new BufferedReader(fileReader);
            bufferedWriter = new BufferedWriter(fileWriter);
            String str;
            if ((str = bufferedReader.readLine()) != null) {
                bufferedWriter.write(str);
            }
        } catch (Exception e) {

        } finally {
            try {
                bufferedReader.close();
                //bufferedWriter.flush();
                bufferedWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


    @Test
    //这种方式可以设置字符集。这样就不会乱码了
    public void inputStreamBufferTest() {
        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter = null;
        File file = new File("F://a.txt");
        File file1 = new File("F://b.txt");
        try {
            bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
            bufferedWriter = new BufferedWriter(new FileWriter(file1));
            String str;
            if ((str = bufferedReader.readLine()) != null) {
                bufferedWriter.write(str);
            }


        } catch (Exception e) {

        } finally {
            try {
                bufferedReader.close();
                bufferedWriter.flush();
                bufferedWriter.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    @Test
    //图片拷贝
    public void copyStreamTest() {
        File file = new File("F://a.jpg");
        File file1 = new File("F://b.jpg");
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try {
            if (!file1.exists()) {
                file1.createNewFile();
            }
            bufferedInputStream = new BufferedInputStream(new FileInputStream(file));
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file1));
            byte[] bt = new byte[512];
            while (bufferedInputStream.read(bt) != -1) {
                bufferedOutputStream.write(bt);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                bufferedOutputStream.flush();
                bufferedInputStream.close();
                bufferedOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @Test
    //对象流输出
    public void objectWrite() {
        Book book = new Book(1, "111");
        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("E://object.txt")));
            objectOutputStream.writeObject(book);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                objectOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    //对象流读入
    public void objectRead() {
        Book book = null;
        ObjectInputStream objectInputStream = null;
        try {
            objectInputStream = new ObjectInputStream(new FileInputStream(new File("E://object.txt")));
            book = (Book) objectInputStream.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                objectInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println(book);
    }

    public void srcRead() {
        File file = new File("d://a.txt");
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String str = null;
            while ((str = reader.readLine()) != null) {
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(inputStream);
        }


    }

}
